package com.ruoyi.web.wuchuan.service.imp;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.web.wuchuan.dto.Feature;
import com.ruoyi.web.wuchuan.dto.PDFProperties;
import com.ruoyi.web.wuchuan.enmu.ModuleTypeEnum;
import com.ruoyi.web.wuchuan.service.ConversionJsonService;
import com.ruoyi.web.wuchuan.util.HttpZipUtils;
import com.ruoyi.web.wuchuan.util.ShapeFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class ConversionJsonServiceImpl implements ConversionJsonService {

    @Autowired
    private ShapeFileUtils shapeFileUtils;

    @Override
    public File downBeforeData(PDFProperties pdfProperties, ModuleTypeEnum moduleTypeEnum) {

        List<Feature> featureData = pdfProperties.getFeatures();
        List<String> shapZipNames = new LinkedList<>();
        String uuid = UUID.randomUUID().toString();
        if(!(pdfProperties.getType().equals("1")||pdfProperties.getType().equals("6")||pdfProperties.getType().equals("7"))) {
            featureData.forEach(feature -> {
                try {
                    String a = feature.getJsonFeature().replace("null", "0");
                    feature.setJsonFeature(a);
                    String shapZipName = shapZip(pdfProperties, feature, uuid);
                    if (StringUtils.isNotEmpty(shapZipName)) {
                        shapZipNames.add(shapZipName);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }else {
            File file = new File(ShapeFileUtils.tempPath + File.separator + uuid +File.separator);
            if (file.exists()) {
                file.delete();
                file.mkdirs();
            } else {
                file.mkdirs();
            }
            if(pdfProperties.getType().equals("6")||pdfProperties.getType().equals("7")){
              String zipPath = this.urlZip(pdfProperties.getType(),file.getPath());
              shapZipNames.add(zipPath);
            }
        }
        //Do shap file generation
        String pdfPath = moduleTypeEnum.get().analyticalDataByType(pdfProperties,uuid);
        shapZipNames.add(pdfPath);
        String zipName = ShapeFileUtils.tempPath + File.separator + uuid + File.separator + moduleTypeEnum.get().getZipName();
        try {
            shapeFileUtils.zips(zipName, "", shapZipNames);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new File(zipName);
    }



    String shapZip(PDFProperties pdfProperties,Feature feature, String uuid) throws IOException {
        File file = shapeFileUtils.createFile(feature.getFileName() + ".shp", uuid);
            String shapPath = file.getPath();
            shapeFileUtils.geojson2Shape(feature.getJsonFeature(), shapPath);
            String zipName = file.getParentFile().getPath() + File.separator + feature.getFileName() + ".zip";
            List<String> list = new ArrayList<>();
            list.add(file.getParentFile().getPath());
            shapeFileUtils.zips(zipName, feature.getFileName(), list);
            return zipName;

    }
    String urlZip(String type,String filePath){
        String zipName = "";
        String httpUrl = "";
        if(type.equals("6")){
            zipName = "宜灌木区域分析报告.zip";
            httpUrl = "http://222.74.215.218:53000/wuChuan/shrub/shrub.zip";
        }else if (type.equals("7")){
            zipName = "宜乔木区分析报告.zip";
            httpUrl = "http://222.74.215.218:53000/wuChuan/arbor/arbor.zip";
        }
        HttpZipUtils.saveUrlAs(httpUrl,filePath,zipName,"GET");
        return filePath + File.separator + zipName;
    }
}
